/**
 * 系统资源修改js
 */
var comValidform=null;
var returnStatus=false;
var sysResourceUpdate={
	dataCache:null,
		
	startEvent:function(){
		
		sysResourceUpdate.showInfoById();
		
		comValidform=$(".com-addForm").Validform({
			tiptype:2,
			tipSweep:true,
			beforeSubmit:function(curform){
				var parentId=sysResourceUpdate.getSelData();
				if(parentId!=null){
					$('.com-addForm input[name="parentId"]').val(parentId);
				}
				
				var url = 'mgr/SysResourceCtrl/updateSysResource';
				var params = jsUtils.serializeJson($('.com-addForm'));
				var cb = function(data){
					alert(data.msg);
					//成功返回
					if(data.resultCode==200){
						//关闭窗口并刷新数据
						art.dialog.close();
						returnStatus=true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});
		
		
		
		
	},
	
	gerAllResource:function(){
		//获取所有资源
		var url ='mgr/SysResourceCtrl/getSysResourceList';
		var params={
			'curPage':0,
			'pageSize':9999
		};
		var cb = function(data){
			sysResourceUpdate.dataCache=data.result;
		};
		sendApiNoLoadSync(url, params, cb, 'POST');
	},
	
	showInfoById:function(){
		sysResourceUpdate.gerAllResource();
		
		var updId=art.dialog.data('updId');  //修改id
		var url = 'mgr/SysResourceCtrl/getSysResourceById';
		var params ={
				'id':updId
		};
		var cb = function(data){
			if(data.resultCode==200){
				var ret=data.result;
				
				for(var i in ret){
					var text=ret[i];
					//alert("result i = " + i + "  text = " + text + " ret[i]=  " + ret[i]);
					if(i=='resType'){
						$('#com-addForm select[name="'+i+'"]').val(text);
						continue;
					}else if(i=='descp'){
						$('#com-addForm textarea[name="'+i+'"]').val(text);
						continue;
					}else if(i=='uri'){
						$('#com-addForm textarea[name="'+i+'"]').val(text);
						continue;
					}
					
					$('#com-addForm input[name="'+i+'"]').val(text);
				}
				sysResourceUpdate.showTreeData();
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	setCheck:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourceTree"),
		py = "",
		sy = "s",
		pn = "",
		sn = "s",
		type = { "Y":py + sy, "N":pn + sn};
		zTree.setting.check.chkboxType = type;
	},
	
	showTreeData:function(){
		var setting = {
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all"
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			view: {
				nameIsHTML: true
			}
		};
		var zNodes = [];
		var dataCache=sysResourceUpdate.dataCache;
		
		var parentId=$('#com-addForm input[name="parentId"]').val();
		for (var i = 0; i < dataCache.length; i++) {
			zNodes[i]={
				id:dataCache[i].id,
				name:dataCache[i].name,
				pId:dataCache[i].parentId
			};
			if(parentId==dataCache[i].id){
				zNodes[i].checked=true;
			};
			if(i==0){
				zNodes[i].open=true;
			}
		}
		$.fn.zTree.init($("#selSysResourceTree"), setting, zNodes);
		sysResourceUpdate.setCheck();
		$("#py").bind("change", sysResourceUpdate.setCheck());
		$("#sy").bind("change", sysResourceUpdate.setCheck());
		$("#pn").bind("change",	sysResourceUpdate.setCheck());
		$("#sn").bind("change",	sysResourceUpdate.setCheck());
	},
	
	//选择返回事件
	getSelData:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourceTree");
		var nodes = zTree.getCheckedNodes();  // 取得所有选中的
		var len=nodes.length;
		if(len!=1){
			return null;
		}
		return nodes[0].id;
	},
	
	init:function(){
		sysResourceUpdate.startEvent();
	}
}
$(document).ready(function() {
	sysResourceUpdate.init();
});
function submitForm(){
	comValidform.submitForm();
	return returnStatus;
}